package com.syaccp.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.syaccp.model.SysUser;
import com.syaccp.request.SysUserLoginRequest;
import com.syaccp.response.MenusResponse;
import com.syaccp.response.SysUserLoginResponse;
import com.syaccp.response.SystemAdminResponse;
import com.syaccp.service.UserService;
import com.syaccp.utils.IpUtils;
import com.syaccp.utils.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("/api/admin")
@Api(tags = "管理端登录服务")
public class LoginController {

	 @Autowired
	    private AuthenticationManager authenticationManager;



    @Resource
    private UserService userService;
    @ApiOperation(value="PC登录")
    @PostMapping(value = "/login", produces = "application/json")
    public Result<SysUserLoginResponse> login(@RequestBody @Validated SysUserLoginRequest sysUserLoginRequest, HttpServletRequest request) {
        String ip = IpUtils.getClientIp(request);
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
                new UsernamePasswordAuthenticationToken(sysUserLoginRequest.getAccount(),sysUserLoginRequest.getPwd());

       //会调用我们自己写的接口 UserDetailsService 实现类UserDetailsServiceImpl
        Authentication authentication=authenticationManager.authenticate(usernamePasswordAuthenticationToken);
       SysUser sysUser = (SysUser)authentication.getPrincipal();

        SysUserLoginResponse systemAdminResponse = userService.login(sysUser, ip);
        return Result.success(systemAdminResponse);
    }


    @PreAuthorize("hasAuthority('admin:info')")
    @ApiOperation(value="获取用户详情")
    @GetMapping(value = "/getAdminInfoByToken")
    public Result<SystemAdminResponse> getAdminInfo() {
        return Result.success(userService.getInfoByToken());
    }


    @PreAuthorize("hasAuthority('admin:logout')")
    @ApiOperation(value="PC登出")
    @GetMapping(value = "/logout")
    public Result<String> logout(HttpServletRequest request, HttpServletResponse response) {
    	boolean flag = userService.logout(request,response);
    	if (flag) {
    		return Result.success("logout success");
    	}else {
    		return Result.fail("logout success");
    	}

    }
    /**
     * 获取登录页图片
     * @return Map<String, Object>
     */
    @ApiOperation(value = "获取登录页图片")
    @RequestMapping(value = "/getLoginPic", method = RequestMethod.GET)
    public Result<Map<String, Object>> getLoginPic() {
        return Result.success(userService.getLoginPic());
    }

    /**
     * 获取管理员可访问目录
     */
    @PreAuthorize("hasAuthority('admin:login:menus')")
    @ApiOperation(value = "获取管理员可访问目录")
    @RequestMapping(value = "/getMenus", method = RequestMethod.GET)
    public Result<List<MenusResponse>> getMenus() {
        return Result.success(userService.getMenus());
    }


}
